<?php

namespace App\Service\User\v1_2_7;

use App\Constants\ErrorCode;
use App\Helpers\AliyunUtil;
use App\Helpers\ConfigUtil;
use App\Model\CloseFriend;
use App\Model\CoinLog;
use App\Model\Message;
use App\Model\ProfileVerifyLog;
use App\Model\Service\ConsumeService;
use App\Model\User;
use App\Model\UserCoin;
use App\Model\UserProfile;
use App\Model\WechatCheckApply;
use App\Service\BaseService;

/**
 * 查看微信
 */
class CheckWechatService extends BaseService
{
    protected function RoVerifyList()
    {
        return [
            'profile_user_id' => 'required|int'
        ];
    }

    public function handle()
    {
        $this->userId = $this->getUserId();
        $profileUserId = $this->request->input('profile_user_id', 0);
        $confirm = $this->request->input('confirm', 0);
        //亲密度
        $closeValue = CloseFriend::getCloseValue($this->userId, $profileUserId);
        $closeValueLmt = 666;
        if ($closeValue < $closeValueLmt) {
            log_user_result($this->userId, "not_close");
            return $this->setError(ErrorCode::CLOSE_VALUE_LMT, "亲密值达{$closeValueLmt}°C可以申请查看她的社交账号，多增加互动，例如文字聊天，视频聊天，语音聊天和送礼物可以更快提高亲密值~", ['close_value' => (string)$closeValue]);
        }

        //未确认，则返回价格
        $costCoin = ConfigUtil::getWechatCheckCoin($profileUserId);

        //是否支付过
        $checkPay = CoinLog::checkConsumeFrom($this->userId, $profileUserId, CoinLog::FROM_CHECK_WECHAT_OUT);
        //是否对方同意申请
        $applyLog = WechatCheckApply::checkLog($this->userId, $profileUserId);
        //是否可以重新申请
        $callApply = !$applyLog || $applyLog->status == WechatCheckApply::APPLY_REFUSE;
        //已支付，已同意
        if ($checkPay && $applyLog && $applyLog->status == WechatCheckApply::APPLY_PASS) {
            $result = ProfileVerifyLog::checkVerified($profileUserId, ProfileVerifyLog::TYPE_WECHAT, 'data');
            $data = json_decode($result->content, true);
            $ret = [
                'pay' => 1,
                'wechat_account' => $data['account'],
                'wechat_qrcode' => AliyunUtil::IMG_HOST . $data['qrcode'],
                'cost_coin' => $costCoin
            ];
            !$confirm && $ret['cost_coin'] = $costCoin;
            log_user_result($this->userId, $ret);
            return $ret;
        }
        //未申请，未支付：显示5200金币提示
        elseif (!$confirm && $callApply) {
            log_user_result($this->userId, "costcoin");
            return ['cost_coin' => $costCoin];
        }
        //已支付，申请中：直接提示toast
        elseif (!$confirm && $applyLog && $applyLog->status == WechatCheckApply::APPLY_ING) {
            log_user_result($this->userId, "applying");
            return $this->setError(ErrorCode::WECHAT_APPLY_SEND, '已发送过查看申请，请耐心等待对方同意！');
        }

        //发送邀请
        if ($callApply) {
            //金币不足
            $myCoin = UserCoin::getUserInfo($this->userId);
            if ($myCoin['total_coin'] < $costCoin) {
                return $this->setError(ErrorCode::COIN_NOT_ENOUGH, '剩余金币不够查看, 可以先充值哦~');
            }
            //支付（主播不直接到账）
            $payRet = ConsumeService::checkWechat(
                $this->userId,
                $profileUserId,
                $costCoin,
                $myCoin,
                false
            );
            //支付结果
            if (!$payRet) return $this->setError(ErrorCode::COST_COIN_ERR, '支付金币发生错误，请稍后重试～');
            //申请记录
            WechatCheckApply::addLog($this->userId, $profileUserId);
            //发送申请消息
            $profile = UserProfile::getProfile($this->userId);
            Message::pushWechatApply($profileUserId, [
                'nick_name' => $profile->nick_name,
                'user_id' => $profile->user_id,
            ]);
            return $this->setError(ErrorCode::WECHAT_APPLY_SEND, '你已成功发送微信查看申请，请等待对方通过！');
        }
        return $this->setError(ErrorCode::WECHAT_APPLY_SEND, '已发送过微信查看申请，耐心等待对方通过哦～');
    }
}
